// DBDataManager.cpp: implementation of the CDBDataManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GameNetManager.h"
#include "DBDataManager.h"
#include "CommFunc.h" 

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDBDataManager* CDBDataManager::m_pInstance = NULL; 
CDBDataManager::CDBDataManager()
{
	m_hSyn=NULL;
	m_hLoginHandle=NULL;
	m_UpdateAccdCB=NULL;
	m_bRefreshRunning=FALSE;
	m_hLoginHandle=CreateEvent(NULL,FALSE,FALSE,_T("LoginHandle"));
	m_arrAcc.RemoveAll();
	m_arrScript.RemoveAll();
	m_arrServer.RemoveAll();
	CString strDataFilePathName=CCommFunc::CreateInstance()->GetBaseFilePath(_T("database.mdb"));
	m_strConneString.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"%s\""), strDataFilePathName); 
	this->LoadData();
	m_hSyn=CreateEvent(NULL,FALSE,FALSE,_T("DBSyn"));
	SetEvent(m_hSyn);
}

CDBDataManager::~CDBDataManager()
{
	m_bRefreshRunning=FALSE;
	m_hLoginHandle=NULL;
	if(m_pInstance!=NULL)
	{
		delete m_pInstance;
		m_pInstance=NULL;
	}
}

CDBDataManager* CDBDataManager::CreateInstance()
{
	if(m_pInstance==NULL)
	{
		m_pInstance=new CDBDataManager(); 
	}
	return m_pInstance;
}

void CDBDataManager::ClearAll()
{
	int iCount=m_arrAcc.GetSize();
	for(int i=0;i<iCount;i++)
	{
		lpAccount pAcc=m_arrAcc.GetAt(i);
		if(pAcc!=NULL)
		{
			delete pAcc;
			pAcc=NULL;
		}
	}
	iCount=m_arrScript.GetSize();
	for(int i=0;i<iCount;i++)
	{
		lpScript pScript=m_arrScript.GetAt(i);
		if(pScript!=NULL)
		{
			delete pScript;
			pScript=NULL;
		}
	}
	iCount=m_arrServer.GetSize();
	for(int i=0;i<iCount;i++)
	{
		lpServer pServer=m_arrServer.GetAt(i);
		if(pServer!=NULL)
		{
			delete pServer;
			pServer=NULL;
		}
	}
}

void CDBDataManager::LoadData()
{
	this->ClearAll();
	WaitForSingleObject(m_hSyn,INFINITE);
	if(!m_adoDataBase.IsOpen())
	{
		m_adoDataBase.Open(this->m_strConneString,_T(""),_T(""));
		if (!m_adoDataBase.IsOpen())
		{
			ResetEvent(m_hSyn);
			AfxMessageBox(this->m_strConneString);
			return;
		}
	}
	CString strTemp=_T("");
	int iCount=-1;
	CADORecordset dbSet(&m_adoDataBase);
	if(dbSet.Query(_T("SELECT * FROM [Account]")) )
	{ 
		while (!dbSet.IsEOF())
		{
			lpAccount pAcc=new Account; 
			iCount++;
			pAcc->m_iAccountID=dbSet.FieldInt(_T("ID"));
			strTemp=dbSet.FieldString(_T("LoginName"));
			_tcscpy(pAcc->m_tcLoginName,strTemp);
			strTemp=dbSet.FieldString(_T("LoginPwd"));
			_tcscpy(pAcc->m_tcLoginPwd,strTemp); 
			pAcc->m_iServerID=dbSet.FieldInt(_T("ServerID"));
			strTemp=dbSet.FieldString(_T("ServerName"));
			_tcscpy(pAcc->m_tcServerName,strTemp);  
			pAcc->m_iScriptID=dbSet.FieldInt(_T("ScriptID"));
			strTemp=dbSet.FieldString(_T("ScriptName"));
			_tcscpy(pAcc->m_tcScriptName,strTemp);
			strTemp=dbSet.FieldString(_T("LastLoginTime")); 
			if(!strTemp.IsEmpty())
			{
				COleDateTime time;
				time.ParseDateTime(strTemp);
				pAcc->m_dateLoginTime=time.m_dt;
			}
			pAcc->m_iIsChecked=dbSet.FieldInt(_T("IsChecked"));
			if(pAcc->m_iIsChecked!=1) pAcc->m_iIsChecked=0;
			pAcc->m_iState=em_UnLogin;
			
			pAcc->m_stRoleInfo.engry=dbSet.FieldInt(_T("EngryValue")); //Read engry from database
			pAcc->m_stRoleInfo.onlinetime=dbSet.FieldDouble(_T("LastOnlineTime"));
			//pAcc->m_memIndex=iCount;
			m_arrAcc.Add(pAcc);
			dbSet.MoveNext();
		}
		dbSet.Close();
	}
	if(dbSet.Query(_T("SELECT * FROM [Script]")))
	{ 
		while (!dbSet.IsEOF())
		{
			lpScript pScript=new Script; 
			pScript->m_iID=dbSet.FieldInt(_T("ID"));

			strTemp=dbSet.FieldString(_T("ScriptName"));
			_tcscpy(pScript->m_tcScriptName,strTemp);

			strTemp=dbSet.FieldString(_T("ScriptFilePath"));
			_tcscpy(pScript->m_tcScriptFilePath,strTemp); 
 
			strTemp=dbSet.FieldString(_T("ScriptFileName"));
			_tcscpy(pScript->m_tcScriptFileName,strTemp);  
 
			strTemp=dbSet.FieldString(_T("FileType"));
			_tcscpy(pScript->m_tcFileType,strTemp);  
 
			strTemp=dbSet.FieldString(_T("External01"));
			_tcscpy(pScript->m_tcExternal01,strTemp);   
 
			strTemp=dbSet.FieldString(_T("External02"));
			_tcscpy(pScript->m_tcExternal02,strTemp);    
 
			strTemp=dbSet.FieldString(_T("External03"));
			_tcscpy(pScript->m_tcExternal03,strTemp);     

			m_arrScript.Add(pScript);
			dbSet.MoveNext();
		}
		dbSet.Close();
	}
	if(dbSet.Query(_T("SELECT * FROM [Server]")))
	{ 
		while (!dbSet.IsEOF())
		{
			lpServer pServer=new Server; 
			pServer->m_iID=dbSet.FieldInt(_T("ID"));

			strTemp=dbSet.FieldString(_T("ServerName"));
			_tcscpy(pServer->m_tcServerName,strTemp);

			strTemp=dbSet.FieldString(_T("ServerIP"));
			_tcscpy(pServer->m_tcServerIP,strTemp); 
 
			strTemp=dbSet.FieldString(_T("ServerPort"));
			_tcscpy(pServer->m_tcServerPort,strTemp);  
 
			strTemp=dbSet.FieldString(_T("ExeFilePath"));
			_tcscpy(pServer->m_tcExeFilePath,strTemp);  
 
			strTemp=dbSet.FieldString(_T("External01"));
			_tcscpy(pServer->m_tcExternal01,strTemp);   
 
			strTemp=dbSet.FieldString(_T("External02"));
			_tcscpy(pServer->m_tcExternal02,strTemp);    
 
			strTemp=dbSet.FieldString(_T("External03"));
			_tcscpy(pServer->m_tcExternal03,strTemp);     

			m_arrServer.Add(pServer);
			dbSet.MoveNext();
		}
		dbSet.Close();
	}
	ResetEvent(m_hSyn);
}

lpAccount CDBDataManager::FindAccByID(int iAccID)
{
	int iCount=m_arrAcc.GetSize();
	for(int i=0;i<iCount;i++)
	{
		lpAccount pAcc= m_arrAcc.GetAt(i);
		if(pAcc!=NULL && pAcc->m_iAccountID==iAccID)
		{
			return pAcc;
		}
	}
	return NULL;
}

lpScript CDBDataManager::FindScriptByID(int iScriptID)
{
	int iCount=m_arrScript.GetSize();
	for(int i=0;i<iCount;i++)
	{
		lpScript pScript= m_arrScript.GetAt(i);

		if(pScript!=NULL && pScript->m_iID==iScriptID)
		{
			return pScript;
		}
	}
	return NULL;
}

lpServer CDBDataManager::FindServerByID(int iServerID)
{
	int iCount=m_arrServer.GetSize();
	for(int i=0;i<iCount;i++)
	{
		lpServer pServer= m_arrServer.GetAt(i);
		if(pServer!=NULL && pServer->m_iID==iServerID)
		{
			return pServer;
		}
	}
	return NULL;
}

	
int CDBDataManager::UpdateAcc(lpAccount pAcc)
{
	if(pAcc==NULL) return -1;
	WaitForSingleObject(m_hSyn,INFINITE);
	ResetEvent(m_hSyn);
	CString strSql=_T("");
	if(pAcc->m_iAccountID<0)
	{ 
		strSql.Format(_T("insert into Account (LoginName,LoginPwd,ServerID,ServerName,ScriptID,ScriptName)values('%s','%s',%d,'%s',%d,'%s')"),pAcc->m_tcLoginName,pAcc->m_tcLoginPwd,pAcc->m_iServerID,pAcc->m_tcServerName,pAcc->m_iScriptID,pAcc->m_tcScriptName);
		CADORecordset dbSet(&m_adoDataBase); 
		if(dbSet.Execute(strSql))
		{
			pAcc->m_iAccountID=GetNewID("Account");
			m_arrAcc.Add(pAcc);
			SetEvent(m_hSyn);
			return 1;
		}
		else
		{
			SetEvent(m_hSyn);
			return -1;
		}
	}
	else if(pAcc->m_iAccountID>0)
	{
		CString strTempTime=_T("");
		if(pAcc->m_dateLoginTime>10)
		{
			COleDateTime time(pAcc->m_dateLoginTime);
			strTempTime=time.Format(_T("%Y-%m-%d %H:%M:%S"));
		}

//		CString strTempTime01=_T("");
//		if(pAcc->m_stRoleInfo.onlinetime>10)
//		{
//			COleDateTime time01(pAcc->m_stRoleInfo.onlinetime);
//			strTempTime01=time01.Format(_T("%Y-%m-%d %H:%M:%S"));
//		}

		CString strengry,stronlinetime;
		strengry.Format(_T("%d"),pAcc->m_stRoleInfo.engry);
		stronlinetime.Format(_T("%d"),pAcc->m_stRoleInfo.onlinetime);

		strSql.Format(_T("update Account SET  LoginName='%s',LoginPwd='%s',ServerID=%d,ServerName='%s',ScriptID=%d,ScriptName='%s',LastLoginTime='%s',IsChecked=%d,LastOnlineTime=%s,EngryValue=%s where ID=%d"),pAcc->m_tcLoginName,pAcc->m_tcLoginPwd,pAcc->m_iServerID,pAcc->m_tcServerName,pAcc->m_iScriptID,pAcc->m_tcScriptName,strTempTime,pAcc->m_iIsChecked,stronlinetime,strengry,pAcc->m_iAccountID);
		CADORecordset dbSet(&m_adoDataBase); 
		if(dbSet.Execute(strSql))
		{
			SetEvent(m_hSyn);
			return 1;
		}
		else
		{
			SetEvent(m_hSyn);
			return -1;
		}
	}
	SetEvent(m_hSyn);
	return -1;
}

int CDBDataManager::UpdateScript(lpScript pScript)
{
	if(pScript==NULL) return -1;
	CString strSql=_T("");
	WaitForSingleObject(m_hSyn,INFINITE);
	ResetEvent(m_hSyn);
	if(pScript->m_iID<0)
	{
		strSql.Format(_T("insert into Script (ScriptName,ScriptFilePath,ScriptFileName,FileType,External01,External02,External03)values('%s','%s',%s,'%s',%s,'%s')"),pScript->m_tcScriptName,pScript->m_tcScriptFilePath,pScript->m_tcScriptFileName,pScript->m_tcFileType,pScript->m_tcExternal01,pScript->m_tcExternal02,pScript->m_tcExternal03);
		CADORecordset dbSet(&m_adoDataBase);
		if(dbSet.Execute(strSql))
		{
			pScript->m_iID=GetNewID("Script");
			m_arrScript.Add(pScript);
			SetEvent(m_hSyn);
			return 1;
		}
		else
		{
			SetEvent(m_hSyn);
			return -1;
		}
	}
	else
	{
		strSql.Format(_T("update Script  SET  ScriptName='%s',ScriptFilePath='%s',ScriptFileName='%s',FileType='%s',External01='%s',External02='%s',External03='%s' where ID=%d"),pScript->m_tcScriptName,pScript->m_tcScriptFilePath,pScript->m_tcScriptFileName,pScript->m_tcFileType,pScript->m_tcExternal01,pScript->m_tcExternal02,pScript->m_tcExternal03,pScript->m_iID);
		CADORecordset dbSet(&m_adoDataBase);
		if(dbSet.Execute(strSql))
		{
			SetEvent(m_hSyn);
			return 1;
		}
		else
		{
			SetEvent(m_hSyn);
			return -1;
		}
	}
	SetEvent(m_hSyn);
} 

int CDBDataManager::BoundServerList(CComboBox* lpCmb)
{
	lpCmb->ResetContent();
	if(lpCmb==NULL) return -1;
	int iCount=m_arrServer.GetSize();
	int iItemIndex=-1;
	for(int i=0;i<iCount;i++)
	{
		lpServer pServer=m_arrServer.GetAt(i);
		if(pServer==NULL) continue;
		iItemIndex=lpCmb->AddString(pServer->m_tcServerName);
		lpCmb->SetItemData(iItemIndex,pServer->m_iID);
	}
	if(lpCmb->GetCount()>0)
	{
		lpCmb->SetCurSel(0);
	}
	return -1;
}

int CDBDataManager::BoundScriptList(CComboBox* lpCmb)
{
	lpCmb->ResetContent();
	if(lpCmb==NULL) return -1;
	int iCount=m_arrScript.GetSize();
	int iItemIndex=-1;
	for(int i=0;i<iCount;i++)
	{
		lpScript pScript=m_arrScript.GetAt(i);
		if(pScript==NULL) continue;
		iItemIndex=lpCmb->AddString(pScript->m_tcScriptName);
		lpCmb->SetItemData(iItemIndex,pScript->m_iID);
	}
	if(lpCmb->GetCount()>0)
	{
		lpCmb->SetCurSel(0);
	}
	return -1;
}

int CDBDataManager::SelectCMBItemByData(CComboBox* lpCmb,int iData)
{ 
	if(lpCmb==NULL) return -1;
	for(int i=0;i<lpCmb->GetCount();i++)
	{
		if(lpCmb->GetItemData(i)==(DWORD)iData)
		{
			lpCmb->SetCurSel(i);
			return i;
		}
	}
	return -1;
}

int CDBDataManager::UpdateServer(lpServer pServer)
{
		if(pServer==NULL) return -1;
		CString strSql=_T("");
		WaitForSingleObject(m_hSyn,INFINITE);
		ResetEvent(m_hSyn);
	if(pServer->m_iID<0)
	{
		strSql.Format(_T("insert into Server (ServerName,ServerIP,ServerPort,ExeFilePath,External01,External02,External03)values('%s','%s',%s,'%s',%s,'%s')"),pServer->m_tcServerName,pServer->m_tcServerIP,pServer->m_tcServerPort,pServer->m_tcExeFilePath,pServer->m_tcExternal01,pServer->m_tcExternal02,pServer->m_tcExternal03);
		CADORecordset dbSet(&m_adoDataBase);
		if(dbSet.Execute(strSql))
		{
			pServer->m_iID=GetNewID("Server");
			m_arrServer.Add(pServer);
			SetEvent(m_hSyn);
			return 1;
		}
		else
		{
			SetEvent(m_hSyn);
			return -1;
		}
	}
	else
	{
		strSql.Format(_T("update Server  SET  ServerName='%s',ServerIP='%s',ServerPort='%s',ExeFilePath='%s',External01='%s',External02='%s',External03='%s' where ID=%d"),pServer->m_tcServerName,pServer->m_tcServerIP,pServer->m_tcServerPort,pServer->m_tcExeFilePath,pServer->m_tcExternal01,pServer->m_tcExternal02,pServer->m_tcExternal03,pServer->m_iID);
		CADORecordset dbSet(&m_adoDataBase);
		if(dbSet.Execute(strSql))
		{
			SetEvent(m_hSyn);
			return 1;
		}
		else
		{
			SetEvent(m_hSyn);
			return -1;
		}
	}
}

int CDBDataManager::GetNewID(CString strTableName)
{
	WaitForSingleObject(m_hSyn,INFINITE);
	ResetEvent(m_hSyn);
	CString strSql=_T("");
	strSql.Format(_T("select Max(ID)+1 from [%s] as NextID"),strTableName);
	CADORecordset dbSet(&m_adoDataBase);
	if(dbSet.Execute(strSql))
	{
		int iNextID=dbSet.FieldLong(_T("NextID"));
		dbSet.Close();
		SetEvent(m_hSyn);
		return iNextID;
	}
	SetEvent(m_hSyn);
	return 1;
}

BOOL CDBDataManager::DeleteByDataID(int iIDData,CString strTableName)
{
	WaitForSingleObject(m_hSyn,INFINITE);
	ResetEvent(m_hSyn);
	CString strtemp=strTableName;
	strtemp.MakeLower();
	CString strSql=_T("");
	strSql.Format(_T("delete from %s where ID=%d"),strTableName,iIDData);
	CADORecordset dbSet(&m_adoDataBase); 
	if(dbSet.Execute(strSql))
	{
		if(strtemp=="account")
		{
			for(int i=0;i<m_arrAcc.GetSize();i++)
			{
				if(((lpAccount)m_arrAcc.GetAt(i))->m_iAccountID==iIDData)
				{
					m_arrAcc.RemoveAt(i);
				}
			}
		}
		else if(strtemp=="script")
		{
			for(int i=0;i<m_arrScript.GetSize();i++)
			{
				if(((lpScript)m_arrScript.GetAt(i))->m_iID==iIDData)
				{
					m_arrScript.RemoveAt(i);
				}
			}
		}
		else if(strtemp=="server")
		{
			for(int i=0;i<m_arrServer.GetSize();i++)
			{
				if(((lpServer)m_arrServer.GetAt(i))->m_iID==iIDData)
				{
					m_arrServer.RemoveAt(i);
				}
			}
		}
		SetEvent(m_hSyn);
		return 1;
	}
	else
	{
		SetEvent(m_hSyn);
		return -1;
	}
	SetEvent(m_hSyn);
}
 